package autoorder.zsxmlv.service;

import autoorder.zsxmlv.mapper.ShopMapper;
import autoorder.zsxmlv.po.Shop;
import common.zsxmlv.enums.ResultEnum;
import common.zsxmlv.vo.Result;
import common.zsxmlv.vo.Table;
import org.apache.regexp.RE;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: ZSXM
 * @Description:
 * @Company: DingZheng
 * @Date: Create by 2018/6/20 10:14
 * @Version: 1.0
 * @Modify by:
 */
@Service
public class ShopService {

    @Resource
    private ShopMapper shopMapper;

    /**
     * 添加店铺
     *
     * @param shop
     * @return
     */
    public Result insert(Shop shop) {
        Result result = new Result();
        if (shop != null && !"".equals(shop.getUrl())) {
            List<Shop> shops = shopMapper.selectByUrl(shop.getUrl());
            if (shops.size() == 0) {
                int insert = shopMapper.insert(shop);
                if (insert > 0) {
                    result.setSuccess(true);
                    result.setCode(ResultEnum.INSERT_SUCCESS.getCode());
                    result.setMsg(ResultEnum.INSERT_SUCCESS.getMsg());
                } else {
                    result.setSuccess(false);
                    result.setCode(ResultEnum.EXIST_OBJECT.getCode());
                    result.setMsg(ResultEnum.EXIST_OBJECT.getMsg());
                }
            } else {
                result.setSuccess(false);
                result.setCode(ResultEnum.INSERT_ERROR.getCode());
                result.setMsg(ResultEnum.INSERT_ERROR.getMsg());
            }
        } else {
            result.setSuccess(false);
            result.setCode(ResultEnum.PARAM_ERROR.getCode());
            result.setMsg(ResultEnum.PARAM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public Result deleteShop(Integer id) {
        Result result = new Result();
        if (id != null && id != 0) {
            Shop shop = shopMapper.selectByPrimaryKey(id);
            if (shop == null) {
                result.setSuccess(false);
                result.setCode(ResultEnum.NOT_FOUND_DELETE_ERROR.getCode());
                result.setMsg(ResultEnum.NOT_FOUND_DELETE_ERROR.getMsg());
            } else {
                int i = shopMapper.deleteByPrimaryKey(id);
                if (i > 0) {
                    result.setSuccess(true);
                    result.setCode(ResultEnum.DELETE_SUCCESS.getCode());
                    result.setMsg(ResultEnum.DELETE_SUCCESS.getMsg());
                } else {
                    result.setSuccess(false);
                    result.setCode(ResultEnum.DELETE_ERROR.getCode());
                    result.setMsg(ResultEnum.DELETE_ERROR.getMsg());
                }
            }
        } else {
            result.setSuccess(false);
            result.setCode(ResultEnum.PARAM_ERROR.getCode());
            result.setMsg(ResultEnum.PARAM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 修改店铺数据
     * @param shop
     * @return
     */
    public Result modifyShop(Shop shop) {
        Result result = new Result();
        if (shop != null && shop.getId() != null) {
            int i = shopMapper.updateByPrimaryKey(shop);
            if (i > 0) {
                result.setSuccess(true);
                result.setMsg(ResultEnum.UPDATE_SUCCESS.getMsg());
                result.setCode(ResultEnum.UPDATE_SUCCESS.getCode());
            } else {
                result.setSuccess(true);
                result.setMsg(ResultEnum.UPDATE_ERROR.getMsg());
                result.setCode(ResultEnum.UPDATE_ERROR.getCode());
            }
        } else {
            result.setSuccess(false);
            result.setCode(ResultEnum.PARAM_ERROR.getCode());
            result.setMsg(ResultEnum.PARAM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 根据店铺ID获取带你普数据
     * @param id
     * @return
     */
    public Result getShopById(Integer id) {
        Result result = new Result();
        if (id != null && id != 0) {
            Shop shop = shopMapper.selectByPrimaryKey(id);
            if (shop != null) {
                result.setSuccess(true);
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg(ResultEnum.SUCCESS.getMsg());
                result.setData(shop);
            } else {
                result.setSuccess(false);
                result.setCode(ResultEnum.NOT_FOUND.getCode());
                result.setMsg(ResultEnum.NOT_FOUND.getMsg());
            }
        } else {
            result.setSuccess(false);
            result.setCode(ResultEnum.PARAM_ERROR.getCode());
            result.setMsg(ResultEnum.PARAM_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @param keyword
     * @return
     */
    public Table selectByPage(Integer page, Integer limit, String keyword) {
        Table table = new Table();
        boolean condition = page != null && page != 0 && limit != null && limit != 0;
        if (condition) {
            HashMap<Object, Object> map = new HashMap<>();
            map.put("startRecord", (page - 1) * limit);
            map.put("pageSize", limit);
            map.put("keyword", keyword);
            List<Shop> shops = shopMapper.selectByPage(map);
            int count = shopMapper.countBycondition(map);
            table.setCount(count);
            table.setData(shops);
        } else {
            table.setMsg(ResultEnum.PARAM_ERROR.getMsg());
            table.setCode(ResultEnum.PARAM_ERROR.getCode());
        }
        return table;
    }

    /**
     * 查询所有店铺
     * @return
     */
    public Table selectByAll() {
        Table table = new Table();
        List<Shop> shops = shopMapper.selectByAll();
        if (shops != null) {
            table.setCount(shops.size());
            table.setData(shops);
        } else {
            table.setMsg(ResultEnum.NOT_FOUND.getMsg());
            table.setCode(ResultEnum.NOT_FOUND.getCode());
        }
        return table;
    }

}
